﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.RF.Package;
using BLL.RF.Warehouse;
using Model.RF.Package;

public partial class ProductPackage_CheckandPackinBox : System.Web.UI.Page
{
    DataTable dt;
    PackageProduct pp;
    PackintoBox pb;
    StockOut so;

    public ProductPackage_CheckandPackinBox()
    {
        dt = new DataTable();
        pp = new PackageProduct();
        pb = new PackintoBox();
        so = new StockOut();
    }

    /// <summary>
    /// 1.产品类型的判定，所有序列号必须一起做
    /// 2.包装的管控，可以单个序列号做
    /// </summary>

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        string packageProductSN = this.TextBox2.Text.Trim();
        if (!string.IsNullOrEmpty(packageProductSN))
        {
            if (IsExist_inListBox(packageProductSN, this.ListBox1))
            {
                string str1 = "成品序列号" + packageProductSN + "已在列表中！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
            else
            {
                this.ListBox1.Items.Add(packageProductSN);
                this.Label1.Text = "共" + this.ListBox1.Items.Count.ToString() + "个";
                this.TextBox2.Text = "";
                this.TextBox2.Focus();
            }
        }
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        string packageProductSN = this.TextBox3.Text.Trim();
        if (!string.IsNullOrEmpty(packageProductSN))
        {
            if (IsExist_inListBox(packageProductSN, this.ListBox2))
            {
                string str1 = "外箱标签" + packageProductSN + "已在列表中！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
            else
            {
                this.ListBox2.Items.Add(packageProductSN);
                this.Label2.Text = "共" + this.ListBox2.Items.Count.ToString() + "个";
                this.TextBox3.Text = "";
                this.TextBox3.Focus();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        this.ListBox2.Items.Clear();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        bool isCheckPIM = this.CheckBox1.Checked;
        decimal pimValue = 0;
        if (isCheckPIM) 
        {
            if (!decimal.TryParse(this.TextBox5.Text.Trim(),out pimValue))
            {
                string str1 = "启用了按交调指标分箱装箱，但设置的交调指标格式不对，请检查！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
        }
        string pid = this.TextBox1.Text.Trim();
        ProductType p = new ProductType();
        Model.ProductTypeInfo pti = p.LoadEntity(pid);
        if (string.IsNullOrEmpty(pti.ProductTypeID))
        {
            string str1 = "产品类型" + pid + "不存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }
        else
        {
            int amount = int.Parse(this.TextBox4.Text.Trim());
            if (this.ListBox1.Items.Count == amount && this.ListBox2.Items.Count == amount)
            {
                string sn_inconsistent = "";
                if (IsSame_inTwoListBox(this.ListBox1, this.ListBox2, out sn_inconsistent))
                {
                    ListItemCollection list = this.ListBox1.Items;
                    BiuldDT(list);
                    PackintoBoxInfo pbi = new PackintoBoxInfo { ID = 0, RecordTime = DateTime.Now, ProductTypeID = pid, UserID = User.Identity.Name, Amount = amount };
                    string message1;
                    if (pb.PackintoBox_CheckandInsert(pbi, dt,isCheckPIM,pimValue, out message1))
                    {
                        message1 = "产品装箱检验成功！";
                    }
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + message1 + "');</script>", false);
                    dt.Dispose();
                    this.ListBox1.Items.Clear();
                    this.ListBox2.Items.Clear();
                }
                else
                {
                    string str1 = sn_inconsistent + "不在外箱标签列表中！";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                    return;
                }
            }
            else
            {
                string str1 = "成品列表中序列号的数量，或标签列表中序列号的数量，与设置的数量不一致！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                return;
            }
        }
    }

    protected bool IsExist_inListBox(string psn, ListBox lb)
    {
        foreach (ListItem item in lb.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected bool IsSame_inTwoListBox(ListBox lb1, ListBox lb2, out string sn_inconsistent)
    {
        foreach (ListItem item in lb1.Items)
        {
            if (!IsExist_inListBox(item.ToString(), lb2))
            {
                sn_inconsistent = item.ToString();
                return false;
            }
        }
        sn_inconsistent = "";
        return true;
    }

    //生成DataTable
    protected void BiuldDT(ListItemCollection list)
    {
        dt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (ListItem item in list)
        {
            dr = dt.NewRow();
            dr["ProductSN"] = item.Text;
            dt.Rows.Add(dr);
        }
    }
}
